<template>
  <div class="words">
    <!-- 导航 -->
    <van-nav-bar title="我的单词本"
                 right-text="新增单词"
                 @click-left="onMenu"
                 @click-right="addWord">
      <template #left>
        <van-icon name="apps-o"
                  size="24" />
      </template>
    </van-nav-bar>
    <!-- 单词列表  -->
    <div class="word-cont">
      <word-item v-for="item in list"
                 :key="item.word_id"
                 :wordmes="item"
                 @refreshs="getfreshs" />
    </div>
    <!-- 分页 -->
    <van-pagination v-model="currentPage"
                    :total-items="wordTotal"
                    :items-per-page="10"
                    mode="simple"
                    @change="pageChange" />

    <!-- 菜单选项 -->
    <transition name="van-fade">
      <div class="main-menu"
           v-show="isShowMenu">
        <div class="aside-menu">
          <h3>
            <div class="box">
              <div class="inner-box">
                {{nickname}}</div>
            </div>
          </h3>
          <ul>
            <li class="menus-item"
                @click="updPwd">
              <van-icon name="lock" />修改密码
            </li>
            <li class="menus-item"
                @click="loginOut">
              <van-icon name="guide-o" />安全退出
            </li>
          </ul>
          <p class="backs"
             @click="isShowMenu=false">
            <van-icon name="revoke" />返回
          </p>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
import wordItem from '@/components/WordItem.vue'
import { getWordList } from '@/api/main'
import { userPhone, delToken, userNickname } from '@/utils/token'
export default {
  components: {
    wordItem
  },
  data() {
    return {
      isShowMenu: false,
      currentPage: 1,
      wordTotal: 0,
      list: [],
      nickname: userNickname(),
    }
  },
  methods: {
    // 打开侧边栏
    onMenu() {
      this.isShowMenu = true
    },
    // 跳转到新增单词页
    addWord() {
      this.$router.push({ path: '/addword' })
    },
    // 获取单词
    async getWordList() {
      let res = await getWordList(userPhone(), this.currentPage)
      this.list = res.result
      this.wordTotal = res.total
    },
    // 删除刷新
    async getfreshs() {
      let res = await getWordList(userPhone(), 1)
      this.list = res.result
      this.wordTotal = res.total
      this.currentPage = 1
    },
    // 改变页数
    async pageChange(value) {
      this.currentPage = value
      let res = await getWordList(userPhone(), this.currentPage)
      this.list = res.result
      this.wordTotal = res.total
    },
    //跳转密码修改页面
    updPwd() {
      this.$router.push('/updpwd')
    },
    // 退出登录
    loginOut() {
      this.$toast('退出成功')
      delToken()
      setTimeout(() => {
        this.$router.push('/login')
      }, 500)
    }
  },
  created() {
    this.getWordList()
  }
}
</script>

<style lang="less" scoped>
.words {
  background-color: #f6f6f6;
  height: 100vh;
  /* 导航栏 */
  ::v-deep .van-nav-bar__content {
    height: 1.6rem;
    background-color: #e87b61;
    /* 左侧图标 */
    .van-nav-bar__left {
      .van-icon {
        color: #fff;
      }
    }
    /* 标题 ，文字颜色*/
    .van-nav-bar__title,
    .van-nav-bar__text {
      font-size: 0.56rem;
      color: #fff;
      letter-spacing: 0.0267rem;
    }
    .van-nav-bar__text {
      font-size: 0.4267rem;
      letter-spacing: 0px;
    }
  }
  /* 单词列表 */
  .word-cont {
    padding: 0 0.1867rem 40px 0.1867rem;
  }
  /* 分页 */
  ::v-deep .van-pagination {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #fff;
    .van-pagination__item,
    .van-pagination__page-desc {
      height: 1.2rem;
      color: #e87b61;
    }
    .van-pagination__item {
      &:active {
        background-color: #e87b61;
        color: #fff;
      }
    }
    .van-pagination__item--disabled {
      background: #fff;
    }
  }
  .main-menu {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    color: #e87b61;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 99;
    .aside-menu {
      position: relative;
      width: 60%;
      height: 100%;
      background-color: #fff;
      h3 {
        display: flex;
        height: 4.2667rem;
        border-bottom: 2px solid #e87b61;
        .box {
          margin: auto;
          display: flex;
          width: 100px;
          height: 100px;
          border-radius: 50%;
          background-color: #e87b61;
          .inner-box {
            display: flex;
            justify-content: center;
            align-items: center;
            margin: auto;
            width: 95%;
            height: 95%;
            border-radius: 50%;
            background-color: #fff;
          }
        }
      }
      ul {
        padding: 0.4rem 0.2667rem;
        .menus-item {
          padding: 0.2133rem 0.4rem;
          font-size: 0.4267rem;
          font-weight: 600;
          .van-icon {
            font-size: 0.4533rem;
            padding-right: 0.1867rem;
          }
        }
      }
      .backs {
        position: absolute;
        bottom: 28px;
        right: 13px;
        font-size: 0.4533rem;
        font-weight: 600;
        .van-icon {
          font-weight: 600;
        }
      }
    }
  }
}
</style>